import pytest
from selenium import webdriver
# from selenium.webdriver.chrome.options import Options

# 浏览器的预置fix
@pytest.fixture
def browser():
    # 01 用例的前置操作
    """无头模式可以放下下面代码
    并在driver初始化的时候加上参数
    options = chrome_options()
    from selenium.webdriver.chrome.options import options
    # 创建Chrome选项对象
    chrome_options = 0
    ptions()
    # 添加无头模式参数chrome _options.add_argument("--headless")chrome_options.add_argument("--disable-gpu")
    """

    options = Options()
    # options.add_argument('--no-sandbox')
    options.add_argument('--headless')
    # options.add_argument('--disable-extensions')
    # options.add_argument('--disable-gpu')
    # options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(options=options,executable_path='/usr/bin/chromedriver')
    curl = driver.current_url
    # 最大化
    driver.maximize_window()
    # # 移动窗口
    # driver.set_window_position(1900,-200)
    # driver.set_window_size(2550,1000)

    # 02 用例执行，返回driver
    yield driver

    # 03 用例后置-关闭浏览器
    driver.quit()


"""验证码--需封装，考虑异常场景"""
# from time import sleep
#
# import ddddocr
# from selenium import webdriver
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
#
# driver = webdriver.Chrome()
# driver.get("http://120.25.127.201:18001/user/register.html")
# driver.maximize_window()
# driver.find_element(By.XPATH, "//input[@id='txtUName']").send_keys('17308190922')
# driver.find_element(By.XPATH, "//input[@id='txtPassword']").send_keys('hu2374145')
#
# def code():
#     # 验证码处理
#     WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//img[@id='chkd']")))
#     driver.find_element(By.XPATH, "//img[@id='chkd']").screenshot('dsw1.png')
#     ocr = ddddocr.DdddOcr()
#     with open('dsw1.png', 'rb') as f:
#         image = f.read()
#     res = ocr.classification(image)
#     driver.find_element(By.XPATH, "//input[@id='TxtChkCode']").send_keys(res)
#     sleep(3)
#     driver.find_element(By.XPATH, "//input[@id='btnRegister']").click()
#     sleep(2)
# while True:
#     code()
#     login_name = driver.find_element(By.XPATH, "//a[@class='mr15']").text
#     print(login_name)
#     if login_name == "17308190922":
#         print("注册成功")
#         break
#     else:
#         driver.find_element(By.XPATH, "//img[@id='chkd']").click()
#         driver.find_element(By.XPATH, "//input[@id='TxtChkCode']").clear()
#         sleep(2)
# driver.quit()
